package com.dgut.lab.controller;

import com.dgut.lab.entity.Answer;
import com.dgut.lab.entity.User;
import com.dgut.lab.service.AnswerService;
import com.dgut.lab.service.UserService;
import com.dgut.lab.util.CommonUtil;
import com.dgut.lab.util.ParamUtil;
import com.dgut.lab.util.ResponseUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class AnswerController {

    @Autowired
    private AnswerService answerService;
    @Autowired
    private UserService userService;

    @PreAuthorize("hasRole('ROLE_STUDENT')")
    @PostMapping("/answer/add/{username}")
    public Object addAnswer(@RequestBody Map<String,Object> answerList,@PathVariable("username") String username) throws ParseException {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");

        String now = dateFormat.format(new Date());
        Date nowParse = dateFormat.parse(now);

        User userQuery = userService.queryByUsername(username);
        if(userQuery.getLastCommitTime()!=null){

            String result = dateFormat.format(userQuery.getLastCommitTime());
            Date resultParse = dateFormat.parse(result);
            if(!resultParse.before(nowParse)){
                return ResponseUtil.fail(1,"您已经打过卡了！不要重复打卡");
            }
        }

        Answer answer = new Answer();
        answer.setAnswers(answerList);
        answer.setUsername(username);
        answer.setDate(new Date());
        Answer answerResult = answerService.addAnswer(answer);
        if (answerResult != null){
            User user = new User();
            user.setUsername(answer.getUsername());
            user.setLastCommitTime(new Date());
            return ResponseUtil.ok();
        }
        else
            return ResponseUtil.fail(1,"添加打卡记录失败");
    }

//    @GetMapping("/answer/{page}/{rows}")
//    public Object getAnswers(@PathVariable Integer page,
//                             @PathVariable Integer rows){
//        Page<Answer> answers = answerService.queryAllByPage(page, rows);
//        if(answers!=null){
//            return ResponseUtil.ok(answers);
//        }
//        return ResponseUtil.fail(4,"打卡记录查询失败！");
//    }

    @PreAuthorize("hasRole('ROLE_STUDENT') or hasRole('ROLE_ADMIN')")
    @GetMapping("/answer/{username}")
    public Object  getAnswerByUsername(@PathVariable("username") String username) throws ParseException {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");

        String now = dateFormat.format(new Date());
        Date nowParse = dateFormat.parse(now);

        User userQuery = userService.queryByUsername(username);
        boolean isFinish = false;
        if((userQuery.getLastCommitTime())!=null){
            String result = dateFormat.format(userQuery.getLastCommitTime());
            Date resultParse = dateFormat.parse(result);
            isFinish = !resultParse.before(nowParse);
        }
        Answer answerResult = answerService.queryByUsernameLasted(username);
        Map<String,Object> map = new HashMap();
        map.put("answers",answerResult!=null ? answerResult.getAnswers() : null);
        map.put("isFinish",isFinish);
        return ResponseUtil.ok(map);
    }

//    @GetMapping("/answer/test")
//    public List<Answer> test() throws ParseException {
//        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
//        Date startParse = dateFormat.parse("2021-06-29");
//        Date endParse = dateFormat.parse("2021-07-01");
////        return answerService.queryUserByCondition("东莞","37.0",startParse,endParse,1,5);
////        return answerService.queryUserByCondition(null,null,startParse,endParse,1,5);
////        return answerService.queryUserByCondition(null,null,startParse,null,1,5);
//        return answerService.queryUserByCondition("",null,null,startParse,endParse);
////        return null;
//    }

    @PreAuthorize("hasRole('ROLE_ADMIN')")
    @PostMapping("/answer/condition")
    public Object queryAnswerByCondition(@RequestBody HashMap<String,Object>map) throws ParseException {
        List<Answer> answers = answerService.queryAnswerByCondition(ParamUtil.queryParameterNormal(map));
        if(answers!=null){
            return ResponseUtil.ok(CommonUtil.answersToListMap(answers));
        }
        return ResponseUtil.fail(-1,"根据条件查询不到记录！");
    }
}
